﻿using System.Threading.Tasks;
using Xproject.Framework.Core;
using Xproject.Framework.Core.DataContext;
using Xproject.Framework.Core.Repositories;
using Model.Util;
using Repository.Util;
using Xproject.Util;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;

namespace Services.Util
{
    public class UploadFileServices:Service
    {
        IRepositoryAsync<UploadFile> m_UploadFileRepository;
       
        public UploadFileServices(IDataContextAsync dataContext) : base(dataContext)
        {
            m_UploadFileRepository = m_UnitOfWork.RepositoryAsync<UploadFile>();           
        }

        public bool AdduUploadFile(UploadFile file, string userId)
        {
            if (file != null)
            {
                if (string.IsNullOrEmpty(file.FileId))
                {
                    file.FileId = Guid.NewGuid().ToString();
                }                
                file.EntityState = EntityState.Added;
                file.CreateTime = DateTime.Now;
                file.Creator = userId.ToString();
                m_UploadFileRepository.Insert(file);
                return m_UnitOfWork.SaveChanges() == 1;
            }
            return false;
        }

        public bool AdduUploadFiles(List<UploadFile> files, string userId)
        {
            if (files != null&& files.Count>0)
            {
                foreach(UploadFile file in files)
                {
                    if (string.IsNullOrEmpty(file.FileId))
                    {
                        file.FileId = Guid.NewGuid().ToString();
                    }
                    file.EntityState = EntityState.Added;
                    file.CreateTime = DateTime.Now;
                    file.Creator = userId.ToString();                 
                }
                m_UploadFileRepository.InsertRange(files);
                return m_UnitOfWork.SaveChanges() == files.Count;
            }                      
            return false;
        }

        public List<UploadFile> GetUploadFiles(string site, List<string> fileids)
        {
            return m_UploadFileRepository.GetUploadFiles(site, fileids);
        }

        public UploadFile GetById(string site, string fileId)
        {
            return m_UploadFileRepository.Get(site, fileId);
        }
    }
}
